<template>
	<view class="loginPage">
		<u-navbar title="登录" :autoBack="true" bgColor="transparent" leftIconColor="#000000"
			titleStyle="color:#000000;"></u-navbar>
		<image :src="loginBg" class="pageBg" v-if="loginBg"></image>
		<view class="wechatAuth">
			<image src="../../static/logo.png" class="wechatImage"></image>
			<view class="wechatButton" @click="validateAuthorize">授权登录</view>
			<view class="cancelButton" @click="cancelLogin">取消登录</view>
			<view class="agreementBox">
				<image src="../../static/common/icon_xuan.png" v-if="!isAagree" @click="isAagree=!isAagree"
					class="select">
				</image>
				<image src="../../static/common/icon_xuan1.png" v-if="isAagree" @click="isAagree=!isAagree"
					class="select">
				</image>

				<view class="text">我已阅读且同意向上生活服务小程序
					<span style="color: #A3C616;"
						@click="$Tips('/pages/richText/richText?type=privacy_policy')">《隐私保护指引》</span>
					和
					<span style="color: #A3C616;"
						@click="$Tips('/pages/richText/richText?type=user_agreement')">《用户协议》</span>
				</view>
			</view>

		</view>
		<Authorization v-model="showAuthorizationModal" @register="userAuthorizeRegister"></Authorization>
	</view>
</template>
<script>
	import Authorization from '@/components/Authorization.vue'
	export default {
		data() {
			return {
				isAagree: false,
				loginBg: 'https://d-xiangshang.bigchun.com/xiangshang/uploads/20241216/8dce5bb43f3dd23e7deac92d16a807ab.png',
				showAuthorizationModal: false, //判断是否需要手机号注册
			};
		},
		components: {
			Authorization
		},
		onLoad() {},
		methods: {
			//取消登录
			cancelLogin() {
				//返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			//用户授权
			validateAuthorize() {
				let that = this
				if (!this.isAagree) {
					return this.$Toast('请阅读并同意《用户服务协议》和《隐私政策》')
				}
				uni.login({
					provider: 'weixin', //使用微信登录
					success: function(loginRes) {
						//验证用户是否注册过
						that.$Api.common.userIsRegister({
							applet_code: loginRes.code,
						}).then(res => {
							if (res.data.is_register == true) {
								that.showAuthorizationModal = true
							} else {
								that.authorizeSuccess(res)
							}
						})
					}
				});
			},
			//授权成功
			authorizeSuccess(res) {
				//授权成功
				this.$store.commit('LOGIN', {
					token: res.data.token
				});
				//获取用户信息并缓存
				this.$Api.user.getUserInfo().then(res => {
					this.$store.commit('UPDATE_USERINFO', res.data.userinfo);
					this.$Tips({
						title: '登录成功！'
					}, {
						tab: 1,
						url: '/pages/index/index'
					})
				})
			},
			//用户注册
			userAuthorizeRegister(registerInfo) {
				let that = this
				uni.login({
					provider: 'weixin', //使用微信登录
					success: function(loginRes) {
						let data = {
							...registerInfo,
							applet_code: loginRes.code,
						}
						console.log(data)
						that.$Api.common.userRegister({
							...registerInfo,
							applet_code: loginRes.code,
						}).then(res => {
							that.authorizeSuccess(res)
						})
					}
				});
			},
		}
	};
</script>
<style lang="scss" scoped>
	.loginPage {
		width: 100%;
		min-height: 100vh;
		position: relative;
		background-color: #ffffff;

		.pageBg {
			position: absolute;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
		}

		.agreementBox {
			display: flex;
			text-align: center;
			justify-content: center;
			width: 100%;
			margin: 30rpx auto 0;
			padding: 0 30rpx;
			line-height: 40rpx;

			.select {
				width: 32rpx;
				height: 32rpx;
			}

			.text {
				flex: 1;
				font-size: 26rpx;
				color: #888888;
			}
		}

		.wechatAuth {
			position: absolute;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 100%;
			z-index: 100;
			top: 360rpx;

			.wechatImage {
				width: 340rpx;
				height: 72rpx;
			}


			.wechatButton {
				margin: 160rpx auto 0;
				width: 620rpx;
				height: 88rpx;
				font-weight: bold;
				font-size: 30rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 88rpx;
				background: #A3C616;
				border-radius: 44rpx;
			}

			.cancelButton {
				margin: 40rpx auto 0;
				width: 620rpx;
				height: 88rpx;
				font-weight: bold;
				font-size: 30rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 88rpx;
				color: #A3C616;
				border-radius: 44rpx;
				border: 2rpx solid #A3C616;
			}
		}
	}
</style>